<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"  @submit.native.prevent @keyup.enter.native="handleQuery">
      <el-form-item label="姓名" prop="qKey">
        <el-input v-model="queryParams.qKey" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="qIds1">
        <el-cascader
          v-model="queryParams.qIds1"
          :options="deptList"
          :props="{value: 'id',children: 'children',label: 'label',expandTrigger:'hover'}"
          filterable
          clearable
          placeholder=""
          style="width: 400px;"
        ></el-cascader>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="姓名" align="center" prop="nickName" fixed/>
      <el-table-column label="部门" align="center" prop="dept.deptName"/>
      <el-table-column label="手机号" align="center" prop="phonenumber"/>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width"  width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['points:poiManagement:poiManagement']"
            @click="handleManage(scope.row)"
          >积分</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



    <el-dialog :title="title" v-if="open" :visible.sync="open" width="50%" top="500vh" append-to-body>
      <div style="max-height: 600px;overflow-y: auto;">
        <el-descriptions title="" :column="3" border :lebalStyle="lebalStyle" :contentStyle="contentStyle">
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              姓名
            </template>
            {{user.nickName}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-s-custom"></i>
              部门
            </template>
            {{user.dept.deptName}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-phone"></i>
              手机号
            </template>
            {{user.phonenumber}}
          </el-descriptions-item>
        </el-descriptions>
        <br>
        <br>

        <el-row>
          <el-date-picker
            v-model="time"
            type="year"
            placeholder=""
          >
          </el-date-picker>
        </el-row>
        <br>

        <el-row>
          <el-col :span="6" v-for="m in 4" :key="m">
            <el-col :span="2" style="height: 1px;"></el-col>
            <el-col :span="20">
              <el-card>
                <h2>第 {{m}} 季度</h2>
                <el-row>
                  <el-col :span="8">
                    <el-button size="mini" type="text" icon="el-icon-view"
                               v-if="quarterList.indexOf(m) >= 0 &&
                               (currentTime.getFullYear() > time.getFullYear() ||
                               currentTime.getFullYear() === time.getFullYear() && currentTime.getMonth()+1 >= 3*(m-1))"
                               v-hasPermi="['points:poiManagement:info']"
                               @click="data(m)"
                    >详情</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button size="mini" type="text" icon="el-icon-download"
                               v-if="quarterList.indexOf(m) >= 0 &&
                               (currentTime.getFullYear() > time.getFullYear() ||
                               currentTime.getFullYear() === time.getFullYear() && currentTime.getMonth()+1 >= 3*(m-1))"
                               v-hasPermi="['points:poiManagement:export']"
                               @click="exportData(m)"
                    >导出</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                               v-if="currentTime.getFullYear() === time.getFullYear() &&
                               currentTime.getMonth()+1 > 3*(m-1) && currentTime.getMonth()+1 <= 3*m"
                               v-hasPermi="['points:poiManagement:add']"
                               @click="add"
                    >评分</el-button>
                  </el-col>
                </el-row>
              </el-card>
            </el-col>
            <el-col :span="2" style="height: 1px;"></el-col>
          </el-col>
        </el-row>
        <br>
      </div>
    </el-dialog>



  </div>
</template>

<script>

import ApplyInfo from "@/views/points/applyInfo/applyInfo";
import {Loading, Message} from "element-ui";
import {blobValidate} from "@/utils/ruoyi";
import errorCode from "@/utils/errorCode";

export default {
  name : "PoiManagement",
  components: {
    ApplyInfo
  },
  dicts: ['apply_status','pass'],
  data() {
    return {
      deptList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        qKey: "",
        qIds1: undefined,
        qTimeStartList: undefined,
        qStatus: undefined,
        pageNum: 1,
        pageSize: 10,
        isAsc:undefined,
        orderByColumn:undefined
      },
      dialogTableVisible:false,

      lebalStyle:{
        width:"10%",
      },
      contentStyle:{
        width:"21%",
      },
      currentTime:new Date(),
      time:new Date(),
      user:undefined,
      quarterList : [],
    };
  },
  created() {
    //this.getList();
    this.getDepts();
  },
  activated() {
    this.getList();
  },
  methods: {
    getDepts(){
      this.request({
        url: '/system/user/deptTree',
        method: 'get',
      }).then(response => {
        this.deptList = response.data;
      });
    },
    getList() {
      this.loading = true;
      this.request({
        url: '/points/poiManagement/list',
        method: 'get',
        params: this.queryParams
      }).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.isAsc = undefined;
      this.queryParams.orderByColumn = undefined;
      this.queryParams.pageNum = 1;
      this.$refs.tables.clearSort();
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = this.getLowLineCase(column.prop);
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    handleManage(row) {
      this.open = true;
      this.title = "管理";
      this.user = row
      this.request({
        url: '/points/poiManagement/haveData',
        method: 'get',
        params: {
          userId: row.userId,
          year: this.time.getFullYear()
        }
      }).then(response => {
        if(response.data){
          this.quarterList = response.data
        }else {
          this.quarterList = []
        }


      });
    },

    add(){
      this.open = false;
      this.$router.push({
        name: 'PoiManagementAdd',
        params: { userId: this.user.userId }  // 需路由配置 name 和动态段
      });
    },
    data(m){
      this.open = false;
      this.$router.push({
        name: 'PoiManagementData',
        params: {
          userId: this.user.userId,
          year: this.time.getFullYear(),
          quarter: m
        }  // 需路由配置 name 和动态段
      });
    },
    exportData(m){

      let downloadLoadingInstance = Loading.service({ text: "正在下载数据，请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
      this.request({
        url: '/points/poiManagement/export',
        method: 'post',
        data: {
          qId: this.user.userId,
          qYear: this.time.getFullYear(),
          qQuarter: m
        },
        responseType: "blob"
      }).then(async (data) => {
        const isBlob = blobValidate(data);
        if (isBlob) {
          const fileName = new Date().getTime()+'.xlsx';
          const blob = new Blob([data])
          saveAs(blob, fileName)

          this.$modal.msgSuccess("导出成功")
          this.open = false;

        } else {
          const resText = await data.text();
          const rspObj = JSON.parse(resText);
          const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
          Message.error(errMsg);
        }
        downloadLoadingInstance.close();
      }).catch((r) => {
        console.error(r)
        Message.error('下载文件出现错误，请联系管理员！')
        downloadLoadingInstance.close();
      })


    }
  }
};
</script>

